package com.Service;

import com.Dao.EngineerDao;
import com.Dao.EngineerSubjectDao;
import com.POJO.Engineer;
import com.POJO.EngineerSubject;
import com.POJO.User;
import com.Service.Imp.EngineerServiceImp;
import com.Utils.UserName;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;
import java.io.File;
import java.io.IOException;
import java.util.List;
import java.util.Map;

@Service
public class EngineerService extends ServiceImpl<EngineerDao,Engineer> implements EngineerServiceImp {
    @Autowired
    private EngineerDao engineerDao;
    @Autowired
    private EngineerSubjectDao engineerSubjectDao;
    /*查找电话号是否已经注册*/
    public boolean EngineerFindTel(Map<String, String> All_Map){
        QueryWrapper<Engineer> engineerQueryWrapper=new QueryWrapper<>();
        engineerQueryWrapper.eq("tel",All_Map.get("tel"));
        return engineerDao.exists(engineerQueryWrapper);
    }

    /*普通用户注册*/
    public int EngineerRegister(String tel, String pwd, List<String> subjectList){
        Engineer engineer=new Engineer();
        engineer.setUsername("工程师"+UserName.generateRandomString(10));
        engineer.setHeadshot("default.jpg");
        engineer.setTel(tel);
        engineer.setPwd(pwd);
        subjectList.forEach(item -> engineerSubjectDao.insert(new EngineerSubject(tel,item)));
        return engineerDao.insert(engineer);
    }
    /**查询用户个人数据
     *
     *
     * @param tel
     * @return
     */
    public Engineer FindEngineer(String tel){
        QueryWrapper<Engineer> QueryWrapper=new QueryWrapper<>();
        QueryWrapper.eq("tel",tel);
        return engineerDao.selectOne(QueryWrapper);
    }


    /**
     * 用户登录判断
     *
     * @param tel
     * @return
     */
    public Engineer Login(String tel,String pwd){
        QueryWrapper<Engineer> QueryWrapper=new QueryWrapper<>();
        QueryWrapper.eq("tel",tel);
        QueryWrapper.eq("pwd",pwd);
        return engineerDao.selectOne(QueryWrapper);
    }
    /**设置用户默认地址
     *
     * @param Map_All
     * @return
     */
    public int EngineeraddAddress(Map<String,String> Map_All){
        String tel =Map_All.get("tel");
        String province =Map_All.get("province");
        String city =Map_All.get("city");
        String area =Map_All.get("area");
        String addressIp =Map_All.get("addressIp");
        String longitude =Map_All.get("longitude");
        String latitude =Map_All.get("latitude");
        Engineer engineer=new Engineer();
        UpdateWrapper<Engineer> wrapper = new UpdateWrapper<>();
        wrapper.set("province",province);
        wrapper.set("city",city);
        wrapper.set("area",area);
        wrapper.set("address_Ip",addressIp);
        wrapper.set("longitude",longitude);
        wrapper.set("latitude",latitude);
        wrapper.eq("tel",tel);
        return engineerDao.update(engineer,wrapper);
    }

    /**
     *  上传头像
     * @param tel
     * @param headshot
     * @return
     */
    public int updataHeadshot(String tel, MultipartFile headshot){
        //原始文件名
        String originalFilename = headshot.getOriginalFilename();//xxx.png
        assert originalFilename != null;
        //对原始名进行截取"."后面的字符
        String suffix = originalFilename.substring(originalFilename.lastIndexOf("."));//.png
        //使用UUID重新生成文件名，防止文件名称重复造成文件覆盖
        String fileName = tel + suffix;//手机号命名
        File basePath = new File("src/main/resources/static/Headshot");
        // 不存在，则创建该文件夹
//            if(!basePath.exists()){basePath.mkdir();}
        String Path = basePath.getAbsolutePath() + File.separator + fileName;
        try {
            File file=new File(Path);
            //将临时文件转存到指定位置
            headshot.transferTo(file);
        } catch (IOException e) {
            e.printStackTrace();
        }
        Engineer engineer=new Engineer();
        UpdateWrapper<Engineer> UpdateWrapper=new UpdateWrapper<>();
        UpdateWrapper.set("headshot",fileName);
        UpdateWrapper.eq("tel",tel);
        return engineerDao.update(engineer,UpdateWrapper);
    }
    public int updataUsername(String tel,String username){
        Engineer engineer=new Engineer();
        UpdateWrapper<Engineer>  userUpdateWrapper=new UpdateWrapper<>();
        userUpdateWrapper.set("username",username);
        userUpdateWrapper.eq("tel",tel);
        return engineerDao.update(engineer,userUpdateWrapper);
    }
    public int updataPwd(String tel,String pwd){
        Engineer engineer=new Engineer();
        UpdateWrapper<Engineer>  userUpdateWrapper=new UpdateWrapper<>();
        userUpdateWrapper.set("pwd",pwd);
        userUpdateWrapper.eq("tel",tel);
        return engineerDao.update(engineer,userUpdateWrapper);
    }
//    生成
}
